package com.xcity.game.equip.template;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.xcity.game.combat.attr.CombatAttribute;
import com.xcity.game.combat.attr.CombatAttributeFilter;
import com.xcity.game.combat.attr.CombatAttributeFormatter;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.equip.EquipBrand;
import com.xcity.game.equip.EquipSuit;
import com.xcity.game.equip.suit4.BaseSuit4;

import gnu.trove.list.TFloatList;
import gnu.trove.list.array.TFloatArrayList;
import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.annotation.Excel;
import naga.x.util.NumberUtils;

/**
 * 套装效果
 * @author yang.li
 *
 */
@Excel(value = "equip.xlsx", sheet = 2)
public class EquipSuitEffectTemplate extends Template {

	/** 品牌(1：爱马仕，2：xxx) */
	protected EquipBrand brand;
	
	/** 品牌图标 */
	protected String icon;

	/** 2件套装效果 */
	protected String suit2;
	protected CombatAttribute[] enhances2; // 2件套增强的属性

	/** 4件套装效果 */
	protected String suit4;
//	protected CombatAttribute[] enhances4;
	protected float[] params4;
	
	public EquipBrand getBrand() {
		return brand;
	}

	public void setBrand(EquipBrand brand) {
		this.brand = brand != null ? brand : null;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getSuit2() {
		return suit2;
	}

	public void setSuit2(String suit2) {
		this.suit2 = suit2;
	}

	public String getSuit4() {
		return suit4;
	}

	public void setSuit4(String suit4) {
		this.suit4 = suit4;
	}

	public CombatAttribute getEnhancedAttribute(EquipSuit suit, CombatAttributeType type) {
		if (suit != EquipSuit.SUIT2) {
			return null;
		}
		CombatAttribute[] attrs = enhances2;//suit == EquipSuit.SUIT2 ? enhances2 : enhances4;
		if (attrs != null) {
			for (CombatAttribute attr : attrs) {
				if (attr.getType() == type) {
					return attr;
				}
			}
		}
		return null;
	}
	
	public float getSuit4Param(int index) {
		return index >= 0 && index < params4.length ? params4[index] : 0f;
	}
	
	@Override
	public void validate() throws TemplateException {
		this.id = brand.ordinal();
		Object[] arr = parseSuit2(suit2);
		enhances2 = (CombatAttribute[]) arr[0];
		suit2 = (String) arr[1];
		arr = parseSuit4(suit4);
//		enhances4 = (CombatAttribute[]) arr[0];
		params4 = (float[]) arr[0];
		suit4 = (String) arr[1];
	}
	
	public Object[] parseSuit2(String suit) {
		Pattern p = Pattern.compile("\\{[^{}]+\\}");
		Matcher m = p.matcher(suit); // eg. 气势+{6=15}%	      击溃对手员工后恢复{30}%能量
		StringBuffer buf = new StringBuffer();
		List<CombatAttribute> attrs = new ArrayList<CombatAttribute>(2);
		while (m.find()) {
			String v = m.group();
			v = v.substring(1, v.length() - 1);
			int n = v.indexOf('=');
			if (n != -1) { // 属性加成
				int _type = NumberUtils.parseInt(v.substring(0, n), -1);
				CombatAttributeType type = CombatAttributeType.forType(_type);
				if (type == null) {
					throw new IllegalArgumentException("装备套装配置错误: id=" + id + ", suit=" + suit);
				}
				String _value = v.substring(n + 1);
				// 数值都是百分比
				float value = NumberUtils.parseFloat(_value) / 100f;
				CombatAttributeFilter filter;
				if (type.formatter == CombatAttributeFormatter.Formatters.THOUSANDFOLD_PERCENT) {
					// 暴击、韧性、闪避、物攻命中、法攻命中乘以千倍累加
					filter = CombatAttributeFilter.Filters.ADD_THOUSANDFOLD;
				} else {
					filter = CombatAttributeFilter.Filters.MULTIPLY;
				}
				CombatAttributeFormatter formatter = CombatAttributeFormatter.Formatters.PERCENT;
				CombatAttribute attr = new CombatAttribute(type, value, filter, formatter);
				attrs.add(attr);
				m.appendReplacement(buf, _value);
			} else {
				m.appendReplacement(buf, v);
			}
		}
		m.appendTail(buf);
		return new Object[] {
				attrs.isEmpty() ? null : attrs.toArray(new CombatAttribute[attrs.size()]),
				buf.toString()
		};
	}
	
	public Object[] parseSuit4(String suit) {
		Pattern p = Pattern.compile("\\{[^{}]+\\}");
		Matcher m = p.matcher(suit); // eg. 气势+{6=15}%	      击溃对手员工后恢复{30}%能量
		StringBuffer buf = new StringBuffer();
		TFloatList params = new TFloatArrayList(2);
		while (m.find()) {
			String v = m.group();
			v = v.substring(1, v.length() - 1);
			m.appendReplacement(buf, v);
			params.add(NumberUtils.parseFloat(v));
		}
		m.appendTail(buf);
		return new Object[] {
				params.isEmpty() ? null : params.toArray(),
				buf.toString()
		};
	}
	
	
}
